﻿
var remoteUrl = "http://localhost:92/demoSql2005/";
var uploadUrl = "http://localhost:92/upload/";
$.extend({
    include: function (file) {
        var files = typeof file == "string" ? [file] : file;
        for (var i = 0; i < files.length; i++) {
            var name = files[i].replace(/^\s|\s$/g, "");
            var att = name.split('.');
            var ext = att[att.length - 1].toLowerCase();
            var isCSS = ext == "css";
            var tag = isCSS ? "link" : "script";
            var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
            var link = (isCSS ? "href" : "src") + "='" + remoteUrl + name + "'";
            if ($(tag + "[" + link + "]").length == 0) document.write("<" + tag + attr + link + "></" + tag + ">");
        }
    }
});

$.include(['css/HttpUploader.css', 'js/HttpUploader.js']);
$.include(['SWFUpload/default.css', 'SWFUpload/swfupload.js', 'SWFUpload/swfupload.queue.js', 'SWFUpload/fileprogress.js', 'SWFUpload/handlers.js']);

var containers=[];
function UploadPanelInit(containerId,fileFilter, fileSizeLimit, filesLimit, isReadOnly, fields) {
    containers[containerId] = new HttpUploaderMgr({
        ContainerId: containerId,
        FileFilter: fileFilter, //文件类型。所有类型：*。自定义类型：jpg,bmp,png,gif,rar,zip,7z,doc
        FileSizeLimit: fileSizeLimit*1024, //自定义允许上传的文件大小，以字节为单位。0表示不限制
        FilesLimit: filesLimit, //文件选择数限制。0表示不限制
        IsReadOnly: isReadOnly,
        Fields: {//get 或 post参数
            SystemId: fields.SystemId, //系统标识
            ModuleId: fields.ModuleId, //系统内模块标识
            BelongId: fields.BelongId, //比如模块内某新闻的唯一标识

            UserName: "f",
            UserPass: "d",
            uid: 0,
            fid: 1
        }
    });
    //初始化大文件上传控件
    containers[containerId].LoadTo(containerId);
    containers[containerId].Init();

    //var isInstalled = true;
    var isInstalled = containers[containerId].Browser.Check();
    if (!isInstalled) {//初始化SWFUpload控件
        var filters = fileFilter.split(',');
        var filterArr=[];
        for (var i = 0; i < filters.length;i++ ) {
            filterArr.push("*." + filters[i]);
        }
        new SWFUpload({
            ContainerId: containerId,
            // File Upload Settings
            file_size_limit: fileSizeLimit,	// 100MB
            file_types: filterArr.join(";"),
            file_types_description: "",

            post_params: {//get 或 post参数
                SystemId: fields.SystemId, //系统标识
                ModuleId: fields.ModuleId, //系统内模块标识
                BelongId: fields.BelongId //比如模块内某新闻的唯一标识
            }
        });
        $(".btnAddFiles").hide();
    }

    if (isReadOnly) {//文件列表是否只读,=true仅有查看列表权限
        $(".liPnlUploader").hide();
    }
}

/*
 * GB2312转UTF8
 * 例：
 * var xx=new GB2312UTF8();
 * var Utf8=xx.Gb2312ToUtf8("你aaa好aaaaa");
 * var Gb2312=xx.Utf8ToGb2312(Utf8);
 * alert(Gb2312);
 */

function GB2312UTF8() {
    this.Dig2Dec = function (s) {
        var retV = 0;
        if (s.length == 4) {
            for (var i = 0; i < 4; i++) {
                retV += eval(s.charAt(i)) * Math.pow(2, 3 - i);
            }
            return retV;
        }
        return -1;
    }
    this.Hex2Utf8 = function (s) {
        var retS = "";
        var tempS = "";
        var ss = "";
        if (s.length == 16) {
            tempS = "1110" + s.substring(0, 4);
            tempS += "10" + s.substring(4, 10);
            tempS += "10" + s.substring(10, 16);
            var sss = "0123456789ABCDEF";
            for (var i = 0; i < 3; i++) {
                retS += "%";
                ss = tempS.substring(i * 8, (eval(i) + 1) * 8);
                retS += sss.charAt(this.Dig2Dec(ss.substring(0, 4)));
                retS += sss.charAt(this.Dig2Dec(ss.substring(4, 8)));
            }
            return retS;
        }
        return "";
    }
    this.Dec2Dig = function (n1) {
        var s = "";
        var n2 = 0;
        for (var i = 0; i < 4; i++) {
            n2 = Math.pow(2, 3 - i);
            if (n1 >= n2) {
                s += '1';
                n1 = n1 - n2;
            }
            else
                s += '0';
        }
        return s;
    }

    this.Str2Hex = function (s) {
        var c = "";
        var n;
        var ss = "0123456789ABCDEF";
        var digS = "";
        for (var i = 0; i < s.length; i++) {
            c = s.charAt(i);
            n = ss.indexOf(c);
            digS += this.Dec2Dig(eval(n));
        }
        return digS;
    }
    this.Gb2312ToUtf8 = function (s1) {
        var s = escape(s1);
        var sa = s.split("%");
        var retV = "";
        if (sa[0] != "") {
            retV = sa[0];
        }
        for (var i = 1; i < sa.length; i++) {
            if (sa[i].substring(0, 1) == "u") {
                retV += this.Hex2Utf8(this.Str2Hex(sa[i].substring(1, 5)));
                if (sa[i].length) {
                    retV += sa[i].substring(5);
                }
            }
            else {
                retV += unescape("%" + sa[i]);
                if (sa[i].length) {
                    retV += sa[i].substring(5);
                }
            }
        }
        return retV;
    }
    this.Utf8ToGb2312 = function (str1) {
        var substr = "";
        var a = "";
        var b = "";
        var c = "";
        var i = -1;
        i = str1.indexOf("%");
        if (i == -1) {
            return str1;
        }
        while (i != -1) {
            if (i < 3) {
                substr = substr + str1.substr(0, i - 1);
                str1 = str1.substr(i + 1, str1.length - i);
                a = str1.substr(0, 2);
                str1 = str1.substr(2, str1.length - 2);
                if (parseInt("0x" + a) & 0x80 == 0) {
                    substr = substr + String.fromCharCode(parseInt("0x" + a));
                }
                else if (parseInt("0x" + a) & 0xE0 == 0xC0) { //two byte
                    b = str1.substr(1, 2);
                    str1 = str1.substr(3, str1.length - 3);
                    var widechar = (parseInt("0x" + a) & 0x1F) << 6;
                    widechar = widechar | (parseInt("0x" + b) & 0x3F);
                    substr = substr + String.fromCharCode(widechar);
                }
                else {
                    b = str1.substr(1, 2);
                    str1 = str1.substr(3, str1.length - 3);
                    c = str1.substr(1, 2);
                    str1 = str1.substr(3, str1.length - 3);
                    var widechar = (parseInt("0x" + a) & 0x0F) << 12;
                    widechar = widechar | ((parseInt("0x" + b) & 0x3F) << 6);
                    widechar = widechar | (parseInt("0x" + c) & 0x3F);
                    substr = substr + String.fromCharCode(widechar);
                }
            }
            else {
                substr = substr + str1.substring(0, i);
                str1 = str1.substring(i);
            }
            i = str1.indexOf("%");
        }

        return substr + str1;
    }
}